/*
 * @Author: yangzonglong
 * @Date: 2021-05-11 10:58:46
 * @version: v1.0.0
 * @Descripttion: 
 * @LastEditors: yangzonglong
 * @LastEditTime: 2021-05-21 14:33:24
 * @Auditor: 
 */
import { serverLoading } from '@library/utils';
import { WorkFlowHeadProps, FormProps } from './../../interface';
import UserProps from '@library/interface/userInfo';

export const getForm = async (id: string): Promise<FormProps | null> => {
  try {
    const result = await serverLoading('/bk/api/fd/Form/findOne', { where: { id } });
    return result;
  } catch (err) {
    return null;
  }
};

export const doDeleteLine = async (id: string) => {
  try {
    return await serverLoading('/bk/api/workflow/WorkFlowHead/delete', { data: { id } });
  } catch (err) {
  }
};


export const findOne = async (workflowCode: string) => {
  try {
    return await serverLoading('/bk/api/workflow/WorkFlowHead/findOne', { where: { workflowCode } });
  } catch (err) {

  }
};

export const doWithdraw = async (id: string) => {
  try {
    return await serverLoading('/bk/mid/workflow/withdrawWorkflow', { id });
  } catch (err) {
  }
};

export const updateWorkflow = async (data: WorkFlowHeadProps) => {
  try {
    const action = data.id ? 'update' : 'add';
    if (!data.status) data.status = '10'; // 保存状态

    const result = await serverLoading(`/bk/api/workflow/WorkFlowHead/${action}`, { data });
    return result;
  } catch (error) {
    return null;
  }
};

export const getWorkflow = async (id: string): Promise<WorkFlowHeadProps | null> => {
  try {
    const result = await serverLoading('/bk/api/workflow/WorkFlowHead/findOne', {
      where: { id }
    });
    return result;
  } catch (error) {
    return null;
  }
};

export const pubWorkflow = async (data: WorkFlowHeadProps) => {
  try {
    await serverLoading('/bk/mid/workflow/publishWorkflow', { data });
    return true;
  } catch (error) {
    return false;
  }
};

export const withdraw = async (id: string) => {
  try {
    await serverLoading('/bk/mid/workflow/withdrawWorkflow', { id });
    return true;
  } catch (error) {
    return false;
  }
};

// 添加角色
export const addRoles = async (data: Array<{ roleName: string; }>) => {
  try {
    await serverLoading('/bk/api/workflow/WorkFlowRole/add', { data });
    return true;
  } catch (error) {
    return false;
  }
};

export const delRole = async (id: string) => {
  try {
    await serverLoading('/bk/api/workflow/WorkFlowRole/delete', { data: { id } });
    return true;
  } catch (error) {
    return false;
  }
};

export const getUsers = async (userIds: string[]): Promise<UserProps[] | null> => {
  try {
    const { rows } = await serverLoading('/bk/api/company/User/find', { where: { id: userIds } });
    return rows;
  } catch (error) {
    return null;
  }
};

export async function getEmailTemplate() {
  try {
    const result = await serverLoading('/bk/api/workflow/EmailTemplate/find', {
      data: {
        pages
          :
          { pageSize: 20, page: 1 }
      }
    });
    return result;
  } catch (error) {
    return null;
  }
}